package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.NtbBase;
import com.jftone.bh.app.model.NtbBaseOrder;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.model.NtbVipCard;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import java.util.List;


@Service
public class BaseOrderService {
	
	private Logger logger = Logger.getLogger(BaseOrderService.class);
	
	@Autowired
	private Dao dao;

	public List<IData<String, Object>> queryList(IData<String, Object> data, Page page) throws Exception {
		List<IData<String, Object>> list = null;
		String userId = data.getString("userId", "");
		String orderNo = data.getString("orderNo", "");
		String status = data.getString("status", "");
		String sTime = data.getString("sTime", "");
		String eTime = data.getString("eTime", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(userId)){
			param.put("USER_ID",userId);
		}
		if(!StringUtil.isBlank(orderNo)){
			param.put("ORDER_NO", orderNo);
		}
		if(!StringUtil.isBlank(status)){
			param.put("STATUS", status);
		}
		if(!StringUtil.isBlank(sTime)){
			param.put("S_TIME", DateUtil.getMillisecond(DateUtil.parseDate(sTime)));
		}
		if(!StringUtil.isBlank(eTime)){
			param.put("E_TIME", DateUtil.getMillisecond(DateUtil.parseDate(eTime)));
		}
		list = dao.query("queryBaseOrderList", param, page);
		param.clear();
		NtbUser ntbUser;
		NtbBase ntbBase;
		for (IData<String, Object> obj:list) {
			param.put("id",obj.getInt("USER_ID"));
			ntbUser = dao.get(NtbUser.class,param);
			if(ntbUser != null){
				obj.put("ACCOUNT", ntbUser.getAccount());
				obj.put("USER_NAME", ntbUser.getUserName());
			}
			param.clear();
			param.put("id",obj.getInt("BID"));
			ntbBase = dao.get(NtbBase.class,param);
			if(ntbBase != null){
				obj.put("BASE_NAME", ntbBase.getBname());
			}
		}
		return list;
	}
	@Transactional
	public int useOrder(long orderId ) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",orderId);
		NtbBaseOrder ntbBaseOrder =dao.get(NtbBaseOrder.class,param);
		if(ntbBaseOrder==null){
			return 1;
		}else if(ntbBaseOrder.getStatus() != SysConst.BASE_ORDER_STATUS_0){
			return 2;
		}else if(ntbBaseOrder.getSubscribeDate() > Integer.parseInt(DateUtil.getNowStr(DateUtil.DIGI_DATE))){
			return 3;
		}else{
			NtbBaseOrder ntbBaseOrderTemp = new NtbBaseOrder();
			ntbBaseOrderTemp.setId(orderId);
			ntbBaseOrderTemp.setStatus(SysConst.BASE_ORDER_STATUS_1);
			ntbBaseOrderTemp.setUseTime(DateUtil.getMillisecond());
			dao.update(ntbBaseOrderTemp);
			param.clear();
			param.put("id",ntbBaseOrder.getBid());
			NtbBase ntbBase =dao.get(NtbBase.class,param);
			NtbBase ntbBaseTemp = new NtbBase();
			ntbBaseTemp.setId(ntbBase.getId());
			ntbBaseTemp.setOrderNum(ntbBase.getOrderNum()+1);
			dao.update(ntbBaseTemp);
		}
		return 0;
	}
}
